//给页面添加水印
let watermark = {}

let setWatermark = (str, renderDom = document.body) => {
    let id = '1.23452384164.123412415'

    if (document.getElementById(id) !== null) {
        document.body.removeChild(document.getElementById(id))
    }
    var can = document.createElement('canvas')
    //水印的大小
    can.width = document.body.clientWidth / 10
    can.height = document.body.clientHeight / 10
    let cans = can.getContext('2d')
    cans.rotate(-15 * Math.PI / 160) //倾斜角度
    cans.font = `${can.width / 10 + 'px Vedana'}`//水印字体大小
    cans.fillStyle = 'rgba(0,0,0, .1)' // 水印的颜色
    cans.textAlign = 'left'
    cans.textBaseline = 'Middle'
    cans.fillText(str, can.width / 3, can.height / 2)

    let div = document.createElement('div')
    div.id = id
    div.style.pointerEvents = 'none'
    div.style.top = '0px'
    div.style.left = '0px'
    div.style.position = 'fixed'
    div.style.zIndex = '100000'
    div.style.width = document.documentElement.clientWidth + 100000 + 'px'
    div.style.height = document.documentElement.clientHeight + 100000 + 'px'
    div.style.background = 'url(' + can.toDataURL('image/png') + ') left top repeat'
    document.body.appendChild(div)
    renderDom.appendChild(div)
    return id
}

// 该方法只允许调用一次
watermark.set = function (text, renderDom) {
    var id = setWatermark(text, renderDom)
    if (!id) {
        setTimeout(function () { watermark.set(text, renderDom) }, 0)
    }
    // onresize监听流浪器窗口的变化
    // window.onresize = function () {
    // 	setWatermark(text,renderDom);
    // };
}
// 在watermark.js文件中
const outWatermark = (id) => {
    if (document.getElementById(id) !== null) {
        const div = document.getElementById(id).remove()
        div.style.visibility = 'hidden'
    }

}
//   清除水印的方法
watermark.out = () => {
    let str = '1.23452384164.123412415'
    outWatermark(str)
    // window.onresize = function () {
    // 	setWatermark();
    // };
    window.removeEventListener("resize", setWatermark);

}

export default watermark;
